package ca.etsmtl.log430.lab3.systemA;

import java.io.PipedReader;
import java.io.PipedWriter;

/**
 * Formats the line of projet to be passed on to the next filter.
 * Receives: <project number> <status> <system> <version> <rate> <state> <description>
 * Writes to its output: <status> <state> <rate> <project number>
 * @author ah95800
 *
 */
public class FormatFilter extends Thread {
	// Declarations

	// Create local pipes to that will connect to upstream filters
	PipedReader inputPipe = new PipedReader();

	// Create local pipes to that will connect to downstream filter
	PipedWriter outputPipe = new PipedWriter();

	public FormatFilter(PipedWriter InputPipe, PipedWriter OutputPipe) {
		try {
			// Connect InputPipes to upstream filters
			this.inputPipe.connect(InputPipe);
			
			System.out.println("FormatFilter:: connected to upstream filters.");
		} catch (Exception Error) {
			System.out.println("FormatFilter:: Error connecting input pipes.");
		} // try/catch

		try {
			// Connect outputPipe to downstream filter
			this.outputPipe = OutputPipe;
			System.out.println("FormatFilter:: connected to downstream filter.");
		} catch (Exception Error) {
			System.out.println("FormatFilter:: Error connecting output pipe.");
		} // catch

	} // Constructor

	// This is the method that is called when the thread is started

	public void run() {
		// Declarations
		boolean done1; // Flag for reading the input pipe

		// Begin process data from the input pipe
		try {
			// Declarations

			// Need to be an array for easy conversion to string
			char[] characterValue1 = new char[1];

			// Indicate when you are done reading on input pipe
			done1 = false;

			// integer value read from the input pipe
			int integerCharacter1;

			// lines of text from input pipe
			String lineOfText = "";

			// Indicate whether line of text is ready to be output
			// to downstream filter
			boolean write1 = false;

			// Loop for reading data

			while (!done1) {
				
				integerCharacter1 = inputPipe.read();
				characterValue1[0] = (char) integerCharacter1;

				if (integerCharacter1 == -1) // pipe #1 is closed
				{
					done1 = true;
					System.out
							.println("FormatFilter:: Input pipe closed.");

					try {
						inputPipe.close();
					} catch (Exception Error) {
						System.out
								.println("FormatFilter:: Error closing input pipe.");
					} // try/catch

				} else {

					if (integerCharacter1 == '\n') // end of line
					{
						System.out.println("FormatFilter:: Received: "
								+ lineOfText + " on input pipe.");
						write1 = true;
					} else {
						lineOfText += new String(characterValue1);
					} // if

				} // if ( IntegerCharacter1 == -1 )

				if (write1) {
					write1 = false;

					try {
						// Format the line of project
						String tab[] = lineOfText.split(" ");
						lineOfText = "";
						lineOfText += tab[1] + " ";
						lineOfText += tab[5] + " ";
						lineOfText += tab[4] + " ";
						lineOfText += tab[0];
						
						System.out.println("FormatFilter:: Sending "
								+ lineOfText + " to output pipe.");
						lineOfText += "\n";
						outputPipe.write(lineOfText, 0, lineOfText.length());
						outputPipe.flush();
					} catch (Exception IOError) {
						System.out.println("FormatFilter:: Write Error.");
					} // try/catch

					lineOfText = "";

				} // if ( Write1 )


			} // while ( !Done1 )

		} // try

		catch (Exception Error) {
			System.out.println("FormatFilter:: Interrupted.");
		} // catch

		try {
			System.out.println("FormatFilter:: output pipe closed.");
			outputPipe.close();
		} catch (Exception Error) {
			System.out.println("FormatFilter:: Error closing pipes");
		} // try/catch

	} // run
}
